<?php
// $Id: index.class.php 123 2011-12-29 10:08:50Z tomqin@gmail.com $

class Test_Controller extends FrontController{

	function init(){
		$this->res_id = intval($this->action);

		$this->zone = Base::loadModule('zone', true);
		$this->zone->initCity();
	}



	function onIndex(){
		$arr = array(
			'template' => '网站页头/页尾模板',
			'shoplogo'  => '店铺 Logo',
			'csvdishes'  => '载入菜品 CSV 文件'
		);

		echo '<ul>';
		foreach($arr as $k => $v){
			echo "\n";
			echo '<li><a href="'.Base::frontUrl('test', $k).'">'.h($v).'</a></li>';
		}
		echo '</ul>';
	}

	function onShopLogo(){

		echo '<table border="1">';
		//店名
		$stmt = DB::query("select res_id, name, picture from `t_store`");
		while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
			printf('<tr><td>%1$d</td><td><a href="/shop/%1$d" target="_blank">%2$s</a></td><td><img alt="%2$s" width="168" height="130" src="'.STATIC_URL.'upload/%3$s" /></td></tr>',
				 $row['res_id'], $row['name'], $row['picture']);
		}
		echo '</table>';
	}

	//统一页头/页尾
	function onCSVDishes(){


		$CSV = array();
		$row = 1;
		$handle = fopen(DATA_PATH."DISH-20120227.csv","r");
		while ($data = fgetcsv($handle, 1000, ",")) {
		    $num = count($data);
		    if($data[1] && $data[2] && $data[3] && $data[4]){
				$res_id = 1000 + intval(trim($data[0]));
				$res_name = trim($data[1]);
				$catname = trim($data[2]);
				$dishname = trim($data[3]);
				$price = floatval($data[4]);

				//计算 id/name 值
				$part_num[$res_id.$res_name] = 1;
				$part_key[$res_id] = 1;
				$part_var[$res_name] = 1;

				if(!isset($CSV[$res_id])){
					$CSV[$res_id] = new Dish_CSV($res_id, $res_name);
				}else{
					if($CSV[$res_id]->res_name != $res_name){
						$CSV[$res_id]->addRepeat($res_name);
					}
				}

				$CSV[$res_id]->addDish($catname, $dishname, $price);
		    }
		    $row++;
		    //if($row > 1000) break;
		}
		fclose($handle);



		$db_name = $csv_name = $csv_repeat = array();
		$handle = fopen(DATA_PATH."DISH-20120227.sql","w");
		foreach($CSV as $csv){
			$csv_name[$csv->res_id] = $csv->res_name;
			$csv_repeat[$csv->res_id] = $csv->repeat;

			foreach($csv->category as $cat_id => $cat_name){
				$txt = sprintf("Insert `t_dish_cat` (res_id, dish_cat_id, dish_cat_name, ordnum) values (%d, %d, '%s', %d); \n",
							$csv->res_id, $cat_id + 1, $cat_name, $cat_id);

				fwrite($handle, $txt, 1024);
			}

			foreach($csv->dishes as $cat_id => $dishes){
				foreach($dishes as $dish){
					$txt = sprintf("Insert `t_dish` (`res_id`, `status`, `dish_cat_id`, `sys_cat_id`, `name`, `subname`, `ename`, `letter`,"
									." `description`, `picture`, `give_weekday`, `ordnum` , `create_uid`, `create_dt`, `update_uid`, `update_dt`, `price`)"
									." values (%d, 1, %d, 0, '%s', '', '', '', '', '', '1,2,3,4,5,6', 0, 0, 0, 0, 0, %s); \n",
									$csv->res_id, $cat_id + 1, $dish[0], $dish[1]);
					fwrite($handle, $txt, 1024);
				}
			}
		}
		fclose($handle);

		$p_num = count($part_num);
		$p_key = count($part_key);
		$p_var = count($part_var);
		printf("总数：<u>%d</u>，序号数量: <u>%d</u>，店铺名称: <u>%d</u> <br />", $p_num, $p_key, $p_var);
		printf("<strong>预计有 <u>%d</u> 个序号重复问题！</strong><hr />", $p_num - $p_key);

		//店名
		$stmt = DB::query("select res_id, name from `t_store`");
		while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
			$db_name[$row['res_id']] = $row['name'];
		}

		$all_key = $db_name + $csv_name;
		echo '<table border="1">';
		foreach($all_key as $key => $var){
			$dbname  = isset($db_name[$key])?$db_name[$key]:'-';
			$csvname = isset($csv_name[$key])?$csv_name[$key]:'-';
			$csvrepeat = $csv_repeat[$key] ? "<font color='red'>『".join(',', $csv_repeat[$key])."』</font>":'';

			if(strpos($dbname, $csvname)===false){
				$color = ' style="background:#fc0"';
			}else{
				$color = '';
			}

			printf("<tr><td>%d</td><td>%s</td><td%s>%s%s</td></tr>", $key, $dbname, $color, $csvname, $csvrepeat);
		}
		printf("<tr style='background:#ccc'><td>总数</td><td align='right'>%d</td><td align='right'>%d</td></tr>",count($db_name),count($csv_name));
		echo '</table>';
	}

	//统一页头/页尾
	function onTemplate(){
		$this->render('test-template');
	}
}



Class Dish_CSV{
	var $res_id;
	var $res_name;
	var $repeat = array();
	var $category = array();
	var $dishes = array();

	function __construct($res_id, $res_name){
		$this->res_id = $res_id;
		$this->res_name = $res_name;
	}

	function addDish($catname, $dishname, $price){
		$catname  = trim($catname);
		$dishname = trim($dishname);
		$price    = floatval($price);
		if(($catid = array_search($catname, $this->category))===false){
			$catid = count($this->category);
			$this->category[$catid] = $catname;
		}
		$this->dishes[$catid][] = array($dishname, $price);
	}

	function addRepeat($repeat){
		if($this->repeat[count($this->repeat)-1] != $repeat)
			$this->repeat[] = $repeat;
	}
}
?>